//
//  AttentionHomeController.m
//  Arts1
//
//  Created by apple on 15/9/6.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "AttentionHomeController.h"
#import "AttentionFriendController.h"
#import "AttentionSchoolController.h"
#import "AttentionStudioController.h"
#import "UserViewController.h"
#import "AttentionModel.h"
#import "AllHeader.h"

@interface AttentionHomeController ()

//存储数据源
@property (nonatomic ,strong) NSMutableArray *arrayData;

//创建好友视图
@property (nonatomic ,strong) AttentionFriendController *friendVC;

//创建高校视图
@property (nonatomic ,strong) AttentionSchoolController *schoolVC;

//创建画室视图
@property (nonatomic ,strong) AttentionStudioController *studioVC;

//记录上一次打开的视图
@property (nonatomic ,strong) UIViewController *lastVC;

@end

@implementation AttentionHomeController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //调用好友视图
    [self creactFriendView];
//    //调用头部视图
    [self creactTitleView];
    
//    [self requestData];
}

- (NSMutableArray *)arrayData
{
    if (_arrayData == nil)
    {
        self.arrayData = [NSMutableArray array];
    }
    return _arrayData;
}

//请求数据
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    
    NSString *key = dic[@"data"][@"key"];
    
    NSDictionary *pardic = [NSDictionary dictionary];
    
    if (uid && key)
    {
        pardic = @{@"uid":uid,
                   @"key":key};
    }

    [manage POST:@"http://arts1.dlab.com.cn/app/myfollowers" parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
                          
             // 请求成功的时候调用这个block
             NSLog(@"数据请求--%@", dic);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}


/**
 *  创建好友视图
 */
- (void)creactFriendView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 0.1);
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 20, 60);
    _friendVC = [[AttentionFriendController alloc] initWithCollectionViewLayout:layout];
    [self.view addSubview:_friendVC.view];
    
    _friendVC.view.frame = CGRectMake(0, 40 * HEIGHT, SCREEN_WIDTH, self.view.height -40 *HEIGHT);
    
    //记录上一次视图
    _lastVC = _friendVC;
}

/**
 *  创建高校视图
 */
- (void)creactSchoolView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 0.1);
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 20, 70);
    _schoolVC = [[AttentionSchoolController alloc] initWithCollectionViewLayout:layout];
    [self.view addSubview:_schoolVC.view];
    
    _schoolVC.view.frame = CGRectMake(0, 40 * HEIGHT, SCREEN_WIDTH, self.view.height - 40 * HEIGHT);
}

/**
 *  创建画室视图
 */
- (void)creactSyudioView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 0.1);
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 20, 70);
    _studioVC = [[AttentionStudioController alloc] initWithCollectionViewLayout:layout];
    [self.view addSubview:_studioVC.view];
    _studioVC.view.frame = CGRectMake(0, 40 * HEIGHT, SCREEN_WIDTH, self.view.height - 40 * HEIGHT);
}

/**
 *  创建关注页面的头视图
 */
- (void)creactTitleView
{

    _titleView = [[AttentionTiltleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    _titleView.backgroundColor = [UIColor whiteColor];
    
    [_titleView.btnFriend addTarget:self action:@selector(tapFriendView:) forControlEvents:UIControlEventTouchUpInside];
    
    [_titleView.btnSchool addTarget:self action:@selector(tapSchoolView:) forControlEvents:UIControlEventTouchUpInside];
    
    [_titleView.btnStudio addTarget:self action:@selector(tapSyudioView:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_titleView];
    [UIView viewShadowColor:_titleView];
    
}

// 点击后改变被点中的按钮的颜色，其他回复原状
- (void)changeColorOfbutton:(NSInteger)index
{
    for (int i = 0; i < 3; i ++)
    {
        // 找到关注按钮下的相应子按钮
        UIButton *attBTN = (UIButton *)[self.view viewWithTag:1070 + i];
       
        if (index == 1070 + i)
        {
            [attBTN setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        }
        else
        {
            [attBTN setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        }
    }
}

/**
 *  好友页面
 *
 */
- (void)tapFriendView:(UIButton *)tapFriendView
{
    //移除上一次加载的视图
    [_lastVC.view removeFromSuperview];
    //创建要显示的视图
    [self creactFriendView];
    
    [self.view bringSubviewToFront:_titleView];
    //记录当前显示的视图
    _lastVC = _friendVC;
    
    [self changeColorOfbutton:tapFriendView.tag];
}

/**
 *  高校页面
 *
 */
- (void)tapSchoolView:(UIButton *)tapSchoolView
{
    //移除上一次加载的视图
    [_lastVC.view removeFromSuperview];
    //创建要显示的视图
    [self creactSchoolView];
     [self.view bringSubviewToFront:_titleView];
    //记录当前显示的视图
    _lastVC = _schoolVC;

    [self changeColorOfbutton:tapSchoolView.tag];
}

/**
 *  画室页面
 *
 */
- (void)tapSyudioView:(UIButton *)tapSyudioView
{
    //移除上一次加载的视图
    [_lastVC.view removeFromSuperview];
    //创建要显示的视图
    [self creactSyudioView];
     [self.view bringSubviewToFront:_titleView];
    //记录当前显示的视图
    _lastVC = _studioVC;
    
    [self changeColorOfbutton:tapSyudioView.tag];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
